package com.gjs.tools.utils;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class Utils {
	public static int[] paging(HttpServletRequest request, int pageSize, int totalSize) {
		String p = request.getParameter("p");
		int currentPage = (p == null ? 1 : Integer.valueOf(p));
		int totalPage = (int) Math.ceil(totalSize / (double) pageSize);
		int beginIndex = currentPage > 3 ? currentPage - 3 : 1;
		int endIndex = beginIndex + 9;
		endIndex = endIndex > totalPage ? totalPage : endIndex;
		List<Integer> pages = new ArrayList<>();
		for (int i = beginIndex; i <= endIndex; i++) {
			pages.add(i);
		}
		if (totalPage == 1)
			pages.clear();
		
		request.setAttribute("currentPage", currentPage);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("beginIndex", beginIndex);
		request.setAttribute("endIndex", endIndex);
		request.setAttribute("pages", pages);
		
		int begin, end;
		int count = totalSize - (currentPage - 1) * pageSize;
		count = count > pageSize ? pageSize : count;
		begin = (currentPage - 1) * pageSize;
		end = begin + count;
		int[] be = {begin, end};
		return  be;
	}
}
